/**
 * @file RuneDetect.h
 * @author enemy1205 (enemy1205@qq.com)
 * @brief 神符识别类
 * @date 2021-09-12
 *
 */
#include "RuneDetect.h"

using namespace std;
using namespace cv;

/**
 * @brief 调试绘制函数
 * @param predicted_center 神符装甲板预测中心
 */
void RuneDetect::draw(const cv::Point2f &predicted_center) {
    /*神符中心绘制*/
    circle(*frame, this->features.back()->getCenter(), 6, Scalar(0, 55, 255), 2);
    if (this->features.back()->getSon1_Features() != nullptr && !this->features.back()->getSon1_Features()->isValid()/*目前暂未刻意储存已激活神符装甲板，故恒真*/) {
        for (int j = 0; j < 4; ++j) {
            line(*frame, this->features.back()->getSon1_Features()->getVertexes()[j],
                 this->features.back()->getSon1_Features()->getVertexes()[(j + 1) % 4], Scalar(0, 0, 255), 2);
        }
        circle(*frame, this->features.back()->getSon1_Features()->getCenter(), 2, Scalar(0, 255, 0), 1);
    }
    /*预测神符中心绘制*/
    /**@note 预测点超出图像外时，绘制点会被限制在图像边界*/
    if (predicted_center != Point2f(0, 0)) {
        circle(*frame, predicted_center, 2, Scalar(26, 152, 255), 2);
    }
}

/**
 * @brief 神符识别接口
 */
void RuneDetect::SearchRune() {
    //查找轮廓
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    findContours(bin, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE);
    //        for (int i = 0; i < contours.size(); ++i) {
    //            cout << hierarchy[i] << endl;
    //        }
    Rune_ptr temp = make_shared<Rune>(contours, hierarchy);
    temp->updateRestInfo();
    if (temp->isValid()) this->features.emplace_back(temp);/**@attention 共享指针才能这样用，否则普通指针temp在函数结束后即被释放*/
}

/**
 * @brief 神符识别构造函数
 * @param img 此帧图像
 */
RuneDetect::RuneDetect(Mat &img) {
    this->frame = &img;
    this->bin = Pretreatment(*frame, Other_param.init_color_mode, Other_param.init_detect_mode, R_param.threshold,
                             R_param.ksize);
    this->SearchRune();
}

